package com.creditcmsplt.util;

import java.io.File;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GetObjectRequest;

/**
 * 上传pdf到oss
 * 
 * @author 雷阳军
 *
 */
public class OssUtil {

	/**
	 * 
	 * @param filePath:本地pdf文件路径
	 * @param endpoint：oss服务url，可以通过注入@Autowired Environment获取系统配置ossEndpoint.server
	 */
	public static void updloadPDF(String filePath,String endpoint,String keyId,String secret,String bucketName,String key) {
		OSSClient ossClient = new OSSClient(endpoint, keyId, secret);
		key=key.lastIndexOf("/")>=0?key.substring(1, key.length()):key;
		ossClient.putObject(bucketName, key, new File(filePath));
		ossClient.shutdown();
	}
	
	
	/**
	 * 
	 * @param filePath:本地pdf文件路径,key:oss端存储位置
	 * @param endpoint：oss服务url，可以通过注入@Autowired Environment获取系统配置ossEndpoint.server
	 */
	public static void downloadPDF(String endpoint,String keyId,String secret,String bucketName,String key,String filePath) {
		OSSClient ossClient = new OSSClient(endpoint, keyId, secret);
		File destFile=new File(filePath);
		if(!destFile.getParentFile().exists()) {
			destFile.getParentFile().mkdirs();
        }  
		ossClient.getObject(new GetObjectRequest(bucketName, key),destFile);
		ossClient.shutdown();
	}
	
	public static void main(String[] args) {
		//updloadPDF("G:/file/abc.png", "oss-cn-shenzhen.aliyuncs.com", "hl4RmCWoev4TT20T", "BxHJN3iQoDSx3sr7vvOtmSyxotWxXD", "urlhost1", "abc.png");
		
		downloadPDF("oss-cn-shenzhen.aliyuncs.com", "LTAIPQ5ks26KngGu", "as3JRGVLV4qPepLWxuMEfb7LiexAXz", "urlhost1", "pdf/excel4hide/d1b705e9656942e99f210d695f5fee51.xls", "G:/file/456.xls");
		
		//downloadPDF("oss-cn-shenzhen.aliyuncs.com", "hl4RmCWoev4TT20T", "BxHJN3iQoDSx3sr7vvOtmSyxotWxXD", "urlhost1", "pdf/excel4hide/1c67e5572e4142969ca7586935380fcc.xls", "G:/file/abc2.xls");
	}

}
